package com.deer.wms.base.system.service.zh.impl;

import com.deer.wms.base.system.dao.zh.WorkTasksMapper;
import com.deer.wms.base.system.model.bill.*;
import com.deer.wms.base.system.model.box.BoxItemDto;
import com.deer.wms.base.system.model.task.BillInTaskInfo;
import com.deer.wms.base.system.model.task.TaskInfo;
import com.deer.wms.base.system.model.task.TaskInfoDto;
import com.deer.wms.base.system.model.ware.CellInfo;
import com.deer.wms.base.system.model.ware.CellInfoDto;
import com.deer.wms.base.system.model.zh.WorkTasks;
import com.deer.wms.base.system.service.zh.WorkTasksService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class WorkTasksServiceImpl implements WorkTasksService {
    @Autowired
    private WorkTasksMapper workTasksMapper;


   @Override
    public void insertBillInWorkTasks(TaskInfo taskInfo) {
        workTasksMapper.insertBillInWorkTasks(taskInfo);
    }

    @Override
    public  List<CellInfo> selectCellCodeFrom() {
        return workTasksMapper.selectCellCodeFrom();
    }

    @Override
    public List<CellInfo> selectCellCodeFromLittle() {
        return workTasksMapper.selectCellCodeFromLittle();
    }

    @Override
    public List<CellInfo> selectCellCodeFromBig() {
        return workTasksMapper.selectCellCodeFromBig();
    }


    @Override
    public void saveWorkTaskByTaskNumber(TaskInfo taskInfo) {
        workTasksMapper.saveWorkTaskByTaskNumber(taskInfo);
    }

    @Override
    public void saveCell3(Integer cellId) {
        workTasksMapper.saveCell3(cellId);
    }

    @Override
    public void saveCell1(Integer cellId) {
        workTasksMapper.saveCell1(cellId);
    }

    @Override
    public void saveCell0(Integer cellId) {
        workTasksMapper.saveCell0(cellId);
    }

    @Override
    public List<TaskInfoDto> findTaskWorkByBatch(WorkTasks workTasks) {
        return workTasksMapper.findTaskWorkByBatch(workTasks);
    }

    @Override
    public void saveBillInWorkTasksById(TaskInfo taskInfo) {
        workTasksMapper.saveBillInWorkTasksById(taskInfo);
    }

    @Override
    public List<CellInfoDto> selectCellCodeFromByitemCode(WorkTasks workTasks) {
        return workTasksMapper.selectCellCodeFromByitemCode(workTasks);
    }

    @Override
    public void insertBillOutWorkTasks(TaskInfoDto taskInfoDto) {
        workTasksMapper.insertBillOutWorkTasks(taskInfoDto);
    }

    @Override
    public int updateStateByBatch(WorkTasks workTasks) {
        return workTasksMapper.updateStateByBatch(workTasks);
    }

    @Override
    public void updateStateByBoxCode(WorkTasks workTasks) {
        workTasksMapper.updateStateByBoxCode(workTasks);

    }

    @Override
    @Transactional
    public Long getTaskNumber() {
        long taskNumber = 0;
        SimpleDateFormat sdf = new SimpleDateFormat();
        sdf.applyPattern("MMdd");
        Date date = new Date();
        Long time = Long.parseLong(sdf.format(date));
        List<TaskInfo> taskInfos = workTasksMapper.getTaskNumber(time);
        if(taskInfos.isEmpty()){
            delTaskNumberAll();
            String timeNumber = String.format("%0"+3+"d",time);
            taskNumber = Long.parseLong(timeNumber+"0001");

        }else {
            taskNumber = taskInfos.get(0).getTaskNumber()+1;

        }
        insertTaskNumber(taskNumber);

        return taskNumber;

    }

    private void delTaskNumberAll() {
        workTasksMapper.delTaskNumberAll();
    }



    @Override
    public Long getBillInNumber() {
        long billInNumber = 0;
        SimpleDateFormat sdf = new SimpleDateFormat();
        sdf.applyPattern("yyyyMMdd");
        Date date = new Date();
        Long time = Long.parseLong(sdf.format(date));
        List<BillInMaster> billInMasters = workTasksMapper.getBillInNumber(time);
        if(billInMasters.isEmpty()){
            String timeNumber = String.format("%0"+4+"d",time);
            billInNumber = Long.parseLong(timeNumber+"0001");
        }else {
            billInNumber = billInMasters.get(0).getInNumber()+1;
        }


        return billInNumber;
    }

    @Override
    public Long getBillOutNumber() {
        long billOutNumber = 0;
        SimpleDateFormat sdf = new SimpleDateFormat();
        sdf.applyPattern("yyyyMMdd");
        Date date = new Date();
        Long time = Long.parseLong(sdf.format(date));
        List<BillOutMaster> billOutMasters = workTasksMapper.getBillOutNumber(time);
        if(billOutMasters.isEmpty()){
            String timeNumber = String.format("%0"+4+"d",time);
            billOutNumber = Long.parseLong(timeNumber+"0001");
        }else {
            billOutNumber = billOutMasters.get(0).getOutNumber()+1;
        }


        return billOutNumber;
    }

    @Override
    public Long getPlanNumber() {

        long planNumber = 0;
        SimpleDateFormat sdf = new SimpleDateFormat();
        sdf.applyPattern("yyyyMMdd");
        Date date = new Date();
        Long time = Long.parseLong(sdf.format(date));
        List<BillOutPlan> billOutPlans = workTasksMapper.getPlanNumber(time);
        if(billOutPlans.isEmpty()){
            String timeNumber = String.format("%0"+4+"d",time);
            planNumber = Long.parseLong(timeNumber+"0001");
        }else {
            planNumber = billOutPlans.get(0).getPlanNumber()+1;
        }

        return planNumber;
    }

    @Override
    public BoxItemDto selectBoxMSGByTaskNumber(Long taskNumber) {
        return workTasksMapper.selectBoxMSGByTaskNumber(taskNumber);
    }

    @Override
    public List<Long> findTaskNumberByBatch(String batch) {
        return workTasksMapper.findTaskNumberByBatch(batch);
    }

    @Override
    public List<String> findDetailItemByBatch(WorkTasks workTasks) {
        return workTasksMapper.findDetailItemByBatch(workTasks);
    }

    @Override
    public void updateTaskNumber(Long taskNumber) {
        workTasksMapper.updateTaskNumber(taskNumber);
    }

    @Override
    public int updateWorkTasksById(TaskInfoDto taskInfoDto) {
        return workTasksMapper.updateWorkTasksById(taskInfoDto);
    }

    @Override
    public void insertTaskNumber(long taskNumber) {
        workTasksMapper.insertTaskNumber(taskNumber);
    }

    @Override
    public CellInfoDto findCellIdByTaskNumber(Long taskNumber) {
        return workTasksMapper.findCellIdByTaskNumber(taskNumber);
    }

    @Override
    public void updateCell(Integer cellId) {
        workTasksMapper.updateCell(cellId);
    }

    @Override
    public CellInfo findCellStateByCode(BoxItemDto boxItemDto) {
        return workTasksMapper.findCellStateByCode(boxItemDto);
    }

    @Override
    public BoxItemDto findBoxItemById(Long id) {
        return workTasksMapper.findBoxItemById(id);
    }

    @Override
    public void updateCellState(String cellCode) {
        workTasksMapper.updateCellState(cellCode);
    }

    @Override
    public void updateCell1(String cellCode) {
        workTasksMapper.updateCell1(cellCode);
    }

    @Override
    public TaskInfo selectWorkTask(Long taskNumber) {
        return workTasksMapper.selectWorkTask(taskNumber);
    }

    @Override
    public void deleteWorkTask(TaskInfo taskInfo) {
        workTasksMapper.deleteWorkTask(taskInfo);
    }

    @Override
    public void insertWorkTaskOld(TaskInfo taskInfo) {
        workTasksMapper.insertWorkTaskOld(taskInfo);
    }

    @Override
    public TaskInfo findOutType(Long taskNumber) {
        return workTasksMapper.findOutType(taskNumber);
    }

    @Override
    public void delByBoxCode(String boxCode) {
        workTasksMapper.delByBoxCode(boxCode);
    }

    @Override
    public int findItemId(Long taskNumber) {
        return workTasksMapper.findItemId(taskNumber);
    }

    @Override
    public List<CellInfo> selectCellCodeFromHeight() {
        return workTasksMapper.selectCellCodeFromHeight();
    }

    @Override
    public void saveDetailId(Integer id, Integer billInDetailId) {
        workTasksMapper.saveDetailId(id,billInDetailId);
    }


}
